##############################################################
## MOD Title: Torrent Thanks & Rating Mod
## MOD Author: RoadTrain < N/A >  http://www.torrentpier.info/
## MOD Version: 0.1
## :        
## Installation Level: Easy
## Installation Time: 10 Minutes
## Files To Edit: includes/init_bb.php
##                templates/default/viewtopic_attach.tpl
##                templates/default/usercp_viewprofile.tpl
##                  language/lang_russian/lang_main.php
##                  attach_mod/displaying_torrent.php
##                  attach_mod/includes/functions_attach.php
##                  attach_mod/includes/functions_delete.php
##                  includes/torrent_userprofile.php
## Included Files:  thanks.php
##                    rate.php
##                    includes/functions_thanks.php
##                    misc/js/ajax.js
##                    images/sps.gif
##                    templates/default/thanks.htm
## License: http://opensource.org/licenses/gpl-license.php GNU General Public License v2
##############################################################

# 
#-----[ SQL QUERY ]---------------------------------
# 
ALTER TABLE `bb_attachments_desc` ADD (
`thanks` mediumint( 8 ) NOT NULL default '0',
`rating_sum` int(11) NOT NULL default '0',
`rating_count` mediumint(8) NOT NULL default '0'
);

CREATE TABLE `bb_attachments_rating` (
  `attach_id` mediumint(8) unsigned NOT NULL default '0',
  `user_id` mediumint(9) NOT NULL default '0',
  `thanked` tinyint(1) NOT NULL default '0',
  `rating` tinyint(1) NOT NULL default '0',
  PRIMARY KEY  (`attach_id`,`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

# 
#-----[ OPEN ]---------------------------------
# 

includes/init_bb.php

# 
#-----[ FIND ]---------------------------------
#

define('BB_ATTACHMENTS',          'bb_attachments');

# 
#-----[ AFTER, ADD ]---------------------------------
#

//Thanks mod
define('BB_ATTACHMENTS_RATING',   'bb_attachments_rating');
//end


# 
#-----[ OPEN ]---------------------------------
# 
templates/default/viewtopic_attach.tpl	  

# 
#-----[ FIND ]---------------------------------
#

<td width="15%" rowspan="4" class="tCenter pad_6">

# 
#-----[ REPLACE WITH ]---------------------------------
#

<td width="15%" rowspan="6" class="tCenter pad_6">

# 
#-----[ FIND ]---------------------------------
#
	<tr class="row1">
		<td>{L_SIZE}:</td>
		<td>{postrow.attach.tor_reged.TORRENT_SIZE}</td>
	</tr>
	
# 
#-----[ AFTER, ADD ]---------------------------------
#
	<tr class="row1">
		<td class="genmed">{L_RATING}:&nbsp;</td>
		<td class="genmed">{postrow.attach.tor_reged.RATING}&nbsp;({L_RATING_VOTES}: {postrow.attach.tor_reged.RATING_VOTES})
		<!-- IF postrow.attach.tor_reged.YOUR_VOTE -->
		{L_YOUR_VOTE}: {postrow.attach.tor_reged.YOUR_VOTE}
		<!-- ELSE -->
		{postrow.attach.tor_reged.DO_VOTE}
		<!-- ENDIF -->
		</td>
	</tr>
	<tr class="row1">
		<script language="JavaScript" type="text/javascript" src="./misc/js/ajax.js"></script>
		<td>{L_THANKED}:</td>
		<td>{postrow.attach.tor_reged.THANKED}</td>
	</tr>
# 
#-----[ OPEN ]---------------------------------
# 
templates/default/usercp_viewprofile.tpl
# 
#-----[ FIND ]---------------------------------
#
		<tr>
			<th>{L_USER_RATIO}:</th>
			<td id="u_ratio" class="gen">
				<!-- IF DOWN_TOTAL_BYTES gt MIN_DL_BYTES -->
				<b class="gen">{USER_RATIO}</b>&nbsp;
				<a class="gen" href="#" onclick="$('#ratio-expl').show(); $(this).hide(); return false;">[?]</a>
				<!-- ELSE -->
				<span class="med">{L_IT_WILL_BE_DOWN} <b>{MIN_DL_FOR_RATIO}</b></span>
				<!-- ENDIF -->
			</td>
		</tr>
# 
#-----[ AFTER, ADD ]---------------------------------
#
		<tr>
			<th>{L_THANK} <b>/</b> {L_THANKED}:</th>
			<td id="u_thanks" class="gen">
				{USER_THANKS_}
			</td>
		</tr>
		
# 
#-----[ OPEN ]---------------------------------
# 
language/lang_russian/lang_main.php

# 
#-----[   ]---------------------------------
#
//Thanks mod
$lang['USER_THANKS'] = ' ' ;
$lang['USER_THANKS_'] = '  :' ;
$lang['USER_THANKED'] = '  :' ;
$lang['THANKED'] = '';
$lang['THANK'] = '(a)';
$lang['THANKS'] = '';
$lang['THANK_LIST'] = '';
//
$lang['RATING'] = '';
$lang['RATING_VOTES'] = '';
$lang['YOUR_VOTE'] = ' ';
$lang['VOTE_COUNTED'] = '';
//
$lang['RATING_1'] = '';
$lang['RATING_2'] = ' ';
$lang['RATING_3'] = '';
$lang['RATING_4'] = '';
$lang['RATING_5'] = '';
//End thanks mod
# 
#-----[ OPEN ]---------------------------------
# 
attach_mod/displaying_torrent.php

# 
#-----[ FIND ]---------------------------------
#

$tor_file_time  = bb_date($attachments['_'. $post_id][$i]['filetime']);

# 
#-----[ AFTER, ADD ]---------------------------------
#

//Thanks mod
$rating_sum     = $attachments['_'. $post_id][$i]['rating_sum'];
$rating_count   = $attachments['_'. $post_id][$i]['rating_count'];
$your_vote      = $attachments['_'. $post_id][$i]['rating'];
$thanks         = $attachments['_'. $post_id][$i]['thanks'];
$thanked        = $attachments['_'. $post_id][$i]['thanked'];
//End

# 
#-----[ FIND ]---------------------------------
#

if ($tor_reged && $tor_info)
{
	$tor_size = ($tor_info['size']) ? $tor_info['size'] : 0;
	$tor_id   = $tor_info['topic_id'];
	$tor_type = $tor_info['tor_type'];

# 
#-----[ AFTER, ADD ]---------------------------------
#
	$rate_html = '';
    for ($r = 5; $r >= 1; $r-- ) {
        $rate_html .= '<input type="radio" name="rate'.$attach_id.'" value="'.$r.'" onClick="ajax_do(\''
      . append_sid("rate.php?a=" . $attach_id . "&v=" . $r ) .'\');" />'
      . '<span onclick="nodeClick(this);">' . $lang['RATING_'.$r] . '</span>';
    }		

# 
#-----[ FIND ]---------------------------------
# 
			'COMPLETED'       => sprintf($lang['DOWNLOAD_NUMBER'], $tor_info['complete_count']),

# 
#-----[ AFTER, ADD ]---------------------------------
#
			//Thanks mod
			'RATING'          => '<span id="VR'.$attach_id.'">'. ($rating_sum ? round($rating_sum/$rating_count,1) : '-') .'</span>',
			'RATING_VOTES'    => '<span id="VC'.$attach_id.'">'. $rating_count .'</span>',
			'THANKED'         => '<span id="VT'.$attach_id.'">'. $thanks .'</span>'
			. ($thanked || ($userdata['user_id'] == $poster_id) ? '' : '<span id="VB'.$attach_id.'">'. '&nbsp; <img src="images/sps.gif" onClick="this.disabled=true;ajax_do(\''
			. append_sid("thanks.php?mode=thank&amp;a=" . $attach_id) .'\');" alt="'. $lang['THANKS'] .'" style="cursor:pointer" /></span>')
			. ($thanks > 0 ? ' &nbsp; (<span id="VL'.$attach_id.'"><a href="#torrent" onClick="ajax_do(\'thanks.php?mode=list&a='.$attach_id.'\');">'. $lang['THANK_LIST'] .'</a></span>)' : ''),
			'YOUR_VOTE'        => $your_vote > 0 ? $your_vote : '',
			'DO_VOTE'          => $userdata['user_id'] == ANONYMOUS ? '' : ('<span id="VD'.$attach_id.'">'. $rate_html .'</span>'),			
			//Thanks mod End


# 
#-----[ OPEN ]---------------------------------
#
attach_mod/includes/functions_attach.php

# 
#-----[ FIND ]---------------------------------
#

function get_attachments_from_post($post_id_array)
{
	global $attach_config
	
# 
#-----[ IN-LINE AFTER, ADD ]---------------------------------
#
, $userdata

# 
#-----[ FIND ]---------------------------------
#
	$attachments = array();

# 
#-----[ AFTER, ADD ]---------------------------------
#
	//Thanks mod
	$user_id = $userdata['user_id'];
	//End
					
# 
#-----[ FIND ]---------------------------------
#

	$sql = 'SELECT a.post_id, d.*
		FROM ' . BB_ATTACHMENTS . ' a, ' . BB_ATTACHMENTS_DESC . " d
		WHERE a.post_id IN ($post_id_array)
			AND a.attach_id = d.attach_id
		ORDER BY d.filetime $display_order"; 

# 
#-----[ REPLACE WITH ]---------------------------------
#

	$sql = 'SELECT a.post_id, d.*'. ($user_id==ANONYMOUS ? '' : ', r.thanked, r.rating') .'
		FROM ' . BB_ATTACHMENTS . ' a join ' . BB_ATTACHMENTS_DESC . " d on a.attach_id = d.attach_id"
		. ($user_id==ANONYMOUS ? '' : ' left join ' . BB_ATTACHMENTS_RATING . ' r on r.attach_id=a.attach_id and r.user_id=' . $user_id) ."
		WHERE a.post_id IN ($post_id_array)
		ORDER BY d.filetime $display_order";
			
# 
#-----[ OPEN ]---------------------------------
#
attach_mod/include/functions_delete.php

# 
#-----[ FIND ]---------------------------------
#
			// Delete torrents
			$sql = "DELETE FROM ". BB_BT_TORRENTS ."
				WHERE attach_id IN(". implode(',', $attach_id_array) .")";

			if (!DB()->sql_query($sql))
			{
				message_die(GENERAL_ERROR, $lang['ERROR_DELETED_ATTACHMENTS'], '', __LINE__, __FILE__, $sql);
			}
			
# 
#-----[ AFTER, ADD ]---------------------------------
#

            // Delete thanks
            $sql = "DELETE FROM ". BB_ATTACHMENTS_RATING ."
                WHERE attach_id IN(". implode(',', $attach_id_array) .")";

            if (!DB()->sql_query($sql))
            {
                message_die(GENERAL_ERROR, $lang['ERROR_DELETED_ATTACHMENTS'], '', __LINE__, __FILE__, $sql);
            }
# 
#-----[ OPEN ]---------------------------------
#
includes/ucp/torrent_userprofile.php
# 
#-----[ FIND ]---------------------------------
#
{
	$template->assign_block_vars('switch_leeching_none', array());
}

# 
#-----[ AFTER, ADD ]---------------------------------
#
require(INC_DIR .'functions_thanks.php');

# 
#-----[ FIND ]---------------------------------
#
	'LEECH_ROWSPAN'    => ($leeching_count) ? 'rowspan="'. ($leeching_count + 1) .'"' : '',
	
# 
#-----[ AFTER, ADD ]---------------------------------
#
	'USER_THANKS_'     => '<b>' . @get_user_thanks($profiledata['user_id']) . '</b> / <b>' . @get_user_thanked($profiledata['user_id']) . '</b> ()',
	'USER_THANKS_LINK' => 'thanks.php?u=' . $profiledata['user_id'],

#
#-----[ SAVE/CLOSE ALL FILES ]---------------------------------
#
# EoM
